﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules;
using KinonekoSoftware.CodariaDB.Utils;

namespace KinonekoSoftware.Studio.Models.Modules
{
    public abstract class GroupPresenter<T> : ModulePresenter where T : ModuleBlock
    {
        private T _AssociatedObject;


        protected void Initialize(T target)
        {
            _AssociatedObject = target ?? CreateDummyInstance();
            OnInitialize(_AssociatedObject);
        }

        protected abstract T CreateDummyInstance();

        protected abstract void OnInitialize(T target);


        protected void RaisePropertyUpdated(string metadata)
        {
            if (string.IsNullOrEmpty(metadata))
            {
                return;
            }

            Queue?.OnNext(new ValueChangedMSG
            {
                Block    = this,
                Element  = null,
                Metadata = metadata,
            });
        }


        protected void RaiseMetadataChanged(string oldValue, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Queue?.OnNext(new MetadataChangedMSG
                {
                    Block     = this,
                    Element   = null,
                    IsRemoved = true,
                    OldSpell  = oldValue,
                    NewSpell  = null,
                });
            }
            else
            {
                Queue?.OnNext(new MetadataChangedMSG
                {
                    Block     = this,
                    Element   = null,
                    IsRemoved = false,
                    OldSpell  = oldValue,
                    NewSpell  = value,
                });
            }
        }

        public T AssociatedObject => _AssociatedObject;

        /// <summary>
        /// 
        /// </summary>
        public string Id => AssociatedObject?.Id ?? ID.Get();

        /// <summary>
        /// 
        /// </summary>
        public string Title
        {
            get => AssociatedObject?.Title;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Title = value;
                RaiseUpdated();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Subtitle
        {
            get => AssociatedObject?.Description;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Description = value;
                RaiseUpdated();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Description
        {
            get => AssociatedObject?.Description;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Description = value;
                RaiseUpdated();
            }
        }
    }
}